/**
 * 判断数据类型
 * @param data
 * **/
export function TypeOf(data: any): string {
  const toString = Object.prototype.toString;
  const map: { [key: string]: string } = {
    '[object Boolean]': 'boolean',
    '[object Number]': 'number',
    '[object String]': 'string',
    '[object Function]': 'function',
    '[object Array]': 'array',
    '[object Date]': 'date',
    '[object RegExp]': 'regExp',
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Object]': 'object',
    '[object FormData]': 'formData'
  };
  return map[toString.call(data)];
}

/**
 * 随机数
 * @param min 最小值
 * @param max 最大值
 * @returns {number}
 * */
export const Random = (min: number = 0, max: number = 10): number => {
  return Math.floor(Math.random() * (max - min + 1) + min);
};

/**
 * 随机字符串
 * @param count 长度
 * @returns {string}
 * */
export const RandString = (count: number): string => {
  const result = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  const resultArr = result.split('');
  let resultStr = '';
  for (let m = 0; m < count; m++) {
    resultStr += resultArr[Random(0, resultArr.length - 1)];
  }
  return resultStr;
};

interface TreeNode {
  [key: string]: any; // 允许动态属性访问
}

/**
 * 树结构转换
 * @param data 树结构
 * @param id 唯一标识
 * @param pid 父级标识
 * @returns {[]}
 * */
export function TreeDataTranslate<T extends TreeNode>(
  data: T[],
  id: string = 'id',
  pid: string = 'pId'
): (T & { children?: T[] })[] {
  const resData: T[] = DeepClone(data);
  const temp: { [key: string]: T & { children?: T[] } } = {};
  let res: (T & { children?: T[] })[] = [];

  for (let i = 0; i < resData.length; i++) {
    temp[resData[i][id]] = resData[i];
  }
  for (let k = 0; k < resData.length; k++) {
    const node = resData[k];
    const parent = temp[node[pid]];

    if (parent && node[id] !== node[pid]) {
      if (!parent.children) {
        parent.children = [];
      }
      parent.children.push(node);
    } else {
      res.push(node);
    }
  }
  return res;
}

/**
 * 深度拷贝
 * @param obj 要拷贝的对象
 * @returns {any} 拷贝后的对象
 * */
export function DeepClone<T>(obj: T): T {
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }
  if (Array.isArray(obj)) {
    return obj.map(DeepClone) as unknown as T;
  }
  const copy = {} as T;
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      copy[key] = DeepClone(obj[key]);
    }
  }
  return copy;
}

/**
 * 复制
 * @param value 要复制的值
 * */
export function Copy(value: string): void {
  let inputDom = document.createElement('input');
  inputDom.style.position = 'fixed';
  inputDom.style.zIndex = '1000000';
  inputDom.value = value;
  document.body.appendChild(inputDom);
  inputDom.select();
  document.execCommand('copy');
  // navigator.clipboard.writeText(value);
  inputDom.style.display = 'none';
  document.body.removeChild(inputDom);
}

/**
 * 防抖
 * @param func 要执行的函数
 * @param wait 等待时间
 * @param immediate 是否立即执行
 * @returns {Function}
 * */
export const Debounce = <T extends (...args: any[]) => any>(
  func: T,
  wait: number,
  immediate?: boolean
): ((...args: Parameters<T>) => void) => {
  if (immediate) {
    func();
  }
  let timeId: ReturnType<typeof setTimeout> | undefined;
  return (...args: Parameters<T>) => {
    clearTimeout(timeId);
    timeId = setTimeout(() => {
      func(...args);
      clearTimeout(timeId);
    }, wait);
  };
};
